home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10129 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  83 lines

  1. Path: inforamp.net!ts26-11
  2. From: rmorin@inforamp.net (Randy Charles Morin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Encapsulation question.
  5. Date: Wed, 06 Mar 96 06:40:06 GMT
  6. Organization: MiddleWorld SoftWare
  7. Message-ID: <4hjbvv$elu@sam.inforamp.net>
  8. References: <4hg701$goi@insosf1.netins.net>
  9. NNTP-Posting-Host: ts26-11.tor.inforamp.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4hg701$goi@insosf1.netins.net>,
  13.    hhowe@trgnet.com (Harold Howe) wrote:
  14. >Does the following violate encapsulation?  Should an object tell a private 
  15. >member the address of other private members?  I am using this on a grander 
  16. >scale, in which the Application class tells a private dialog object the 
  17. >address of a private configuration structure?  The dialog class needs access 
  18. >to the structure so it can modify it, and this is how I tell the dialog where 
  19. >to look.  Aside from using globals, does anyone have any better suggestions?
  20.  
  21. It's difficult to envision all the reasons you might want to do this, but in 
  22. everycase you are violating fundamentalist (at times like this, we hate those 
  23. red-tape guys) encapsulation rules.  A better solution is to think of the 
  24. private configuration structure as a separate object.  
  25.  
  26.     class MyInteger
  27.     {
  28.     private:
  29.         int i;
  30.         ...
  31.     public:
  32.         int GetJ();
  33.         void SetJ(int iInit);
  34.         ...
  35.     }
  36.  
  37.     class Application
  38.     {
  39.     private:
  40.         MyInteger * j;
  41.         Dialog *dlg;
  42.     public:
  43.         Application(void)  
  44.             { j=new MyInteger();dlg = new Dialog(&j);  }
  45.         ~Application()
  46.             { delete j;}
  47.     }
  48.  
  49.     class Dialog
  50.     {
  51.     private:
  52.         MyInteger * j;
  53.     public:
  54.         Dialog(MyInteger * jInit) {  j=jInit; } ;
  55.     }
  56.  
  57. or a simplified...
  58.  
  59.     class Application
  60.       {
  61.       private:
  62.         int *j;
  63.         Dialog *dlg;
  64.       public:
  65.         Application(void)  { j=new int; dlg = new Dialog(&j);  }
  66.         ~Application() { delete j; }
  67.       }
  68.  
  69.     class Dialog
  70.       {
  71.       private:
  72.         int * j;
  73.         ... // plus other members which access ptr
  74.       public:
  75.         Dialog(int * jInit) {  j=jInit } ;
  76.       }
  77.  
  78. I hope I didn't error anywhere!
  79. I hope you understand it too!
  80. I hope it helps you, even more!
  81.  
  82. Agrivar
  83.